#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：V2 
@File    ：ShengChengShiLi.py
@IDE     ：PyCharm 
@Author  ：郭星
@Date    ：2025/10/15 23:32 
'''
import os
import json
import numpy as np
from typing import Dict, Optional


class GasSchedulingInstanceGenerator:
    """生成不同规模的气体调度问题实例"""

    @staticmethod
    def generate_instance(
            num_workpieces: int,
            num_components: int,
            num_inflation_eq: int,
            num_analysis_eq_per_component: int,
            num_clean_eq: int,
            num_shake_eq: int,
            clean_time: int = 5,
            shake_unit_time: int = 2,
            min_inflation_time: int = 5,
            max_inflation_time: int = 15,
            min_analysis_time: int = 3,
            max_analysis_time: int = 10,
            save_path: Optional[str] = None
    ) -> Dict:
        """生成单个实例"""
        # 生成充装时间
        inflation_time = np.random.randint(
            min_inflation_time,
            max_inflation_time + 1,
            size=num_workpieces
        )

        # 生成分析时间
        analysis_time = np.random.randint(
            min_analysis_time,
            max_analysis_time + 1,
            size=(num_workpieces, num_components)
        )

        instance = {
            "problem_type": "gas_scheduling",
            "parameters": {
                "num_workpieces": num_workpieces,
                "num_components": num_components,
                "num_inflation_eq": num_inflation_eq,
                "num_analysis_eq_per_component": num_analysis_eq_per_component,
                "num_clean_eq": num_clean_eq,  # 新增清洗设备数量
                "num_shake_eq": num_shake_eq  # 新增摇匀设备数量
            },
            "processing_times": {
                "inflation_time": inflation_time.tolist(),
                "analysis_time": analysis_time.tolist(),
                "clean_time": clean_time,  # 新增清洗时间
                "shake_unit_time": shake_unit_time  # 新增摇匀单位时间
            },
            "metadata": {
                "min_inflation_time": min_inflation_time,
                "max_inflation_time": max_inflation_time,
                "min_analysis_time": min_analysis_time,
                "max_analysis_time": max_analysis_time
            }
        }

        if save_path:
            # 确保目录存在
            os.makedirs(os.path.dirname(save_path), exist_ok=True)
            with open(save_path, 'w', encoding='utf-8') as f:
                json.dump(instance, f, ensure_ascii=False, indent=2)
            print(f"实例已保存至: {save_path}")

        return instance

    @staticmethod
    def generate_multiple_scales(base_dir: str = "gas_scheduling_instances"):
        """生成不同规模的实例集"""
        # 定义5种不同规模的参数配置
        scales = [
            # 小型规模
            {
                "scale_name": "small",
                "params": {
                    "num_workpieces": 5,
                    "num_components": 2,
                    "num_inflation_eq": 2,
                    "num_analysis_eq_per_component": 1,
                    "num_clean_eq": 1,
                    "num_shake_eq": 1
                },
                "time_params": {
                    "clean_time": 5,
                    "shake_unit_time": 2,
                    "min_inflation_time": 5,
                    "max_inflation_time": 15,
                    "min_analysis_time": 3,
                    "max_analysis_time": 10
                }
            },
            # 中型规模1
            {
                "scale_name": "medium1",
                "params": {
                    "num_workpieces": 10,
                    "num_components": 3,
                    "num_inflation_eq": 3,
                    "num_analysis_eq_per_component": 2,
                    "num_clean_eq": 2,
                    "num_shake_eq": 2
                },
                "time_params": {
                    "clean_time": 5,
                    "shake_unit_time": 2,
                    "min_inflation_time": 5,
                    "max_inflation_time": 15,
                    "min_analysis_time": 3,
                    "max_analysis_time": 10
                }
            },
            # 中型规模2
            {
                "scale_name": "medium2",
                "params": {
                    "num_workpieces": 20,
                    "num_components": 4,
                    "num_inflation_eq": 4,
                    "num_analysis_eq_per_component": 2,
                    "num_clean_eq": 3,
                    "num_shake_eq": 3
                },
                "time_params": {
                    "clean_time": 5,
                    "shake_unit_time": 2,
                    "min_inflation_time": 5,
                    "max_inflation_time": 20,
                    "min_analysis_time": 3,
                    "max_analysis_time": 12
                }
            },
            # 大型规模
            {
                "scale_name": "large",
                "params": {
                    "num_workpieces": 30,
                    "num_components": 6,
                    "num_inflation_eq": 5,
                    "num_analysis_eq_per_component": 3,
                    "num_clean_eq": 4,
                    "num_shake_eq": 4
                },
                "time_params": {
                    "clean_time": 5,
                    "shake_unit_time": 2,
                    "min_inflation_time": 5,
                    "max_inflation_time": 20,
                    "min_analysis_time": 3,
                    "max_analysis_time": 15
                }
            },
            # 超大型规模
            {
                "scale_name": "xlarge",
                "params": {
                    "num_workpieces": 50,
                    "num_components": 8,
                    "num_inflation_eq": 7,
                    "num_analysis_eq_per_component": 4,
                    "num_clean_eq": 5,
                    "num_shake_eq": 5
                },
                "time_params": {
                    "clean_time": 5,
                    "shake_unit_time": 2,
                    "min_inflation_time": 5,
                    "max_inflation_time": 25,
                    "min_analysis_time": 3,
                    "max_analysis_time": 15
                }
            }
        ]

        # 为每种规模生成3个实例（增加统计显著性）
        for scale in scales:
            scale_dir = os.path.join(base_dir, scale["scale_name"])
            for i in range(3):  # 每个规模生成3个不同实例
                instance_path = os.path.join(scale_dir, f"instance_{i + 1}.json")
                GasSchedulingInstanceGenerator.generate_instance(
                    **scale["params"],
                    **scale["time_params"],
                    save_path=instance_path
                )

        print(f"所有实例已生成，保存至: {os.path.abspath(base_dir)}")


if __name__ == "__main__":
    # 生成不同规模的实例集
    GasSchedulingInstanceGenerator.generate_multiple_scales()
